home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP14 / RNDRCTMT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  3.2 KB  |  105 lines

  1. /*------------------------------------------
  2.    RNDRCTMT.C -- Displays Random Rectangles
  3.                  (c) Charles Petzold, 1996
  4.   ------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <process.h>
  8. #include <stdlib.h>
  9.  
  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. HWND hwnd ;
  13. int  cxClient, cyClient ;
  14.  
  15. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  16.                     PSTR szCmdLine, int iCmdShow)
  17.      {
  18.      static char szAppName[] = "RndRctMT" ;
  19.      MSG         msg ;
  20.      WNDCLASSEX  wndclass ;
  21.  
  22.      wndclass.cbSize        = sizeof (wndclass) ;
  23.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  24.      wndclass.lpfnWndProc   = WndProc ;
  25.      wndclass.cbClsExtra    = 0 ;
  26.      wndclass.cbWndExtra    = 0 ;
  27.      wndclass.hInstance     = hInstance ;
  28.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  29.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  30.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  31.      wndclass.lpszMenuName  = NULL ;
  32.      wndclass.lpszClassName = szAppName ;
  33.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  34.  
  35.      RegisterClassEx (&wndclass) ;
  36.  
  37.      hwnd = CreateWindow (szAppName, "Random Rectangles",
  38.                           WS_OVERLAPPEDWINDOW,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           NULL, NULL, hInstance, NULL) ;
  42.  
  43.      ShowWindow (hwnd, iCmdShow) ;
  44.      UpdateWindow (hwnd) ;
  45.  
  46.      while (GetMessage (&msg, NULL, 0, 0))
  47.           {
  48.           TranslateMessage (&msg) ;
  49.           DispatchMessage (&msg) ;
  50.           }
  51.  
  52.      return msg.wParam ;
  53.      }
  54.  
  55. VOID Thread (PVOID pvoid)
  56.      {
  57.      HBRUSH hBrush ;
  58.      HDC    hdc ;
  59.      int    xLeft, xRight, yTop, yBottom, iRed, iGreen, iBlue ;
  60.  
  61.      while (TRUE)
  62.           {
  63.           if (cxClient != 0 || cyClient != 0)
  64.                {
  65.                xLeft   = rand () % cxClient ;
  66.                xRight  = rand () % cxClient ;
  67.                yTop    = rand () % cyClient ;
  68.                yBottom = rand () % cyClient ;
  69.                iRed    = rand () & 255 ;
  70.                iGreen  = rand () & 255 ;
  71.                iBlue   = rand () & 255 ;
  72.  
  73.                hdc = GetDC (hwnd) ;
  74.                hBrush = CreateSolidBrush (RGB (iRed, iGreen, iBlue)) ;
  75.                SelectObject (hdc, hBrush) ;
  76.  
  77.                Rectangle (hdc, min (xLeft, xRight), min (yTop, yBottom),
  78.                                max (xLeft, xRight), max (yTop, yBottom)) ;
  79.  
  80.                ReleaseDC (hwnd, hdc) ;
  81.                DeleteObject (hBrush) ;
  82.                }
  83.           }
  84.      }
  85.  
  86. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  87.      {
  88.      switch (iMsg)
  89.           {
  90.           case WM_CREATE :
  91.                _beginthread (Thread, 0, NULL) ;
  92.                return 0 ;
  93.  
  94.           case WM_SIZE :
  95.                cxClient = LOWORD (lParam) ;
  96.                cyClient = HIWORD (lParam) ;
  97.                return 0 ;
  98.  
  99.           case WM_DESTROY :
  100.                PostQuitMessage (0) ;
  101.                return 0 ;
  102.           }
  103.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  104.      }
  105.